//
//  WHRecord.m
//  RunningMan
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 JohnnyB. All rights reserved.
//

#import "WHRecord.h"

@interface WHRecord ()<NSCoding>

@property (nonatomic, strong) NSDate *startTime;
@property (nonatomic, strong) NSDate *endTime;
@property (nonatomic, strong) NSMutableArray *locationArray;
@property (nonatomic, assign) double diatance;
@property (nonatomic, assign) CLLocationCoordinate2D *coords;

@end

@implementation WHRecord

#pragma mark - interface

- (CLLocation *)startLocation{
    
    return [self.locationArray firstObject];
    
}

- (CLLocation *)endLocation{
    
    return  [self.locationArray lastObject];
    
}


-(void)addLocation:(CLLocation *)location{
    
    _endTime = [NSDate date];
    [self.locationArray addObject:location];
    
}

-(CLLocationCoordinate2D *)coordinates{
    
    if (self.coords != NULL) {
        free(self.coords);
        self.coords = NULL;
    }
    
    self.coords = (CLLocationCoordinate2D *)malloc(self.locationArray.count *sizeof(CLLocationCoordinate2D));
    
    for (int i = 0; i < self.locationArray.count; i++) {
        CLLocation *location = self.locationArray[i];
        self.coords[i] = location.coordinate;
    }
    
    return self.coords;
    
}

-(NSString *)title{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"king"];
    formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
    return [formatter stringFromDate:self.startTime];
    
    
}

-(NSString *)subTitle{
    
    return [NSString stringWithFormat:@"point:%ld, distance:%.2fm, duration: %.2fs", self
            .locationArray.count, [self totalDistance], [self totalDuration]];
    
}


-(CLLocationDistance)totalDistance{
    
    CLLocationDistance distance = 0;
    
    if (self.locationArray.count > 1) {
        CLLocation *currentLocation = [self.locationArray firstObject];
        
        for (CLLocation *location in self.locationArray) {
            distance += [location distanceFromLocation:currentLocation];
            currentLocation = location;
        }
    }
    return distance;
}


-(NSTimeInterval)totalDuration{
    
    return [self.endTime timeIntervalSinceDate:self.startTime];
    
}

-(NSInteger)numOfLocations{
    
    return self.locationArray.count;
    
}


#pragma mark - NSCoding Protocol
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super init];
    if (self) {
        self.startTime = [aDecoder decodeObjectForKey:@"startTime"];
        self.endTime = [aDecoder decodeObjectForKey:@"endTime"];
        self.locationArray = [aDecoder decodeObjectForKey:@"locations"];
    }
    return  self;
}

-(void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:self.startTime forKey:@"startTime"];
    [aCoder encodeObject:self.endTime forKey:@"endTime"];
    [aCoder encodeObject:self.locationArray forKey:@"locations"];
    
}

-(id)init{
    self = [super init];
    if (self) {
        _startTime = [NSDate date];
        _endTime = _startTime;
        _locationArray = [[NSMutableArray alloc] init];
    }
    
    return self;
}





@end
